package com.ruoyi.zhjd.laboratory.modules.asphaltCombustionFurnace.service.impl;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.zhjd.laboratory.modules.asphaltCombustionFurnace.entity.AsphaltCombustionFurnaceEntity;
import com.ruoyi.zhjd.laboratory.modules.asphaltCombustionFurnace.mapper.AsphaltCombustionFurnaceMapper;
import com.ruoyi.zhjd.laboratory.modules.asphaltCombustionFurnace.service.AsphaltCombustionFurnaceResService;
import com.ruoyi.zhjd.laboratory.modules.asphaltCombustionFurnace.service.AsphaltCombustionFurnaceService;
import com.ruoyi.zhjd.laboratory.modules.errorRecord.service.ErrorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("asphaltCombustionFurnaceService")
public class AsphaltCombustionFurnaceServiceImpl extends ServiceImpl<AsphaltCombustionFurnaceMapper, AsphaltCombustionFurnaceEntity> implements AsphaltCombustionFurnaceService {

    @Autowired
    private AsphaltCombustionFurnaceMapper asphaltCombustionFurnaceMapper;

//    @Autowired
//    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private AsphaltCombustionFurnaceResService asphaltCombustionFurnaceResService;

    @Autowired
    private ErrorRecordService errorRecordService;

    private static Map<String,String> currentMap = new HashMap<>();

    /**
     * 列表
     */
    @Override
    public IPage<JSONObject> queryPage(Map<String, Object> params) {
//        IPage<AsphaltCombustionFurnaceEntity> page = baseServiceComponent.getPage(params, AsphaltCombustionFurnaceEntity.class);
        return asphaltCombustionFurnaceMapper.queryPage(null,params);
    }

    /**
     * 信息
     * @return AsphaltCombustionFurnaceEntity
     */
    @Override
    public AsphaltCombustionFurnaceEntity getInfo(String id){
        return getById(id);
    }

    /**
     * 保存
     */
    @Override
    public void saveData(AsphaltCombustionFurnaceEntity asphaltCombustionFurnace){
        save(asphaltCombustionFurnace);
    }

    /**
     * 修改
     */
    @Override
    public void updateData(AsphaltCombustionFurnaceEntity asphaltCombustionFurnace){
        updateById(asphaltCombustionFurnace);
    }

    /**
     * 删除单条数据
     * @param id
     */
    @Override
    public void deleteOneData(String id){
        removeById(id);
    }

    /**
     * 删除
     */
    @Override
    public void deleteData(List<String> ids){
        removeByIds(ids);
    }

    /**
     * 解析燃烧法沥青含量测定仪（燃烧炉）数据
     * @param resData 试验结果数据
     * @param deviceId 试验设备id
     */
    @Override
    public void parsingData(String resData,String deviceId){
//        Date nowDate = new Date();
//        try {
//            List<String> dataList = ZhjdIotUtils.analyzeExperimentalData(resData);
//            //如果数据以00开头，可能设备刚开机或重启，表示一次新的实验开始
//            if(StringUtils.equals(dataList.get(0),"00")){
//                currentMap.remove(deviceId);
//            }
//
//            //验证位不进行校验
//            int i = 4;
//            //试验数据
//            if (StringUtils.equals(ZhjdIotUtils.DATA_TYPE_TEST, dataList.get(i))) {
//                AsphaltCombustionFurnaceEntity entity = new AsphaltCombustionFurnaceEntity();
//                entity.setDeviceId(deviceId);
//                if(currentMap.get(deviceId) == null){
//                    AsphaltCombustionFurnaceResEntity resEntity = new AsphaltCombustionFurnaceResEntity();
//                    resEntity.setDeviceId(deviceId);
//                    asphaltCombustionFurnaceResService.save(resEntity);
//                    currentMap.put(deviceId,resEntity.getId());
//                }
//                //设置实验结果数据id
//                entity.setResId(currentMap.get(deviceId));
//                entity.setOriginalData(resData);
//                entity.setCreateTime(nowDate);
//                i++;
//                entity.setFlag(dataList.get(i));
//
//                // 将16进制转换为10进制数据，不含（0x）
//                // 初始重量WEIGHT1，4字节无符号数，高字节在前，单位g，包括1位小数；
//                i++;
//                String originalWeight = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16) + Integer.parseInt(dataList.get(i + 2),16) + Integer.parseInt(dataList.get(i + 3),16);
//                entity.setOriginalWeight(ZhjdIotUtils.subByDecimalPlaces(1,originalWeight));
//
//                //实时重量WEIGHT2，4字节无符号数，高字节在前，单位g，包括1位小数；
//                i += 4;
//                String realTimeWeight = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16) + Integer.parseInt(dataList.get(i + 2),16) + Integer.parseInt(dataList.get(i + 3),16);
//                entity.setRealTimeWeight(ZhjdIotUtils.subByDecimalPlaces(1,realTimeWeight));
//
//                i += 2;
//                String realTimeTemp = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16);
//                entity.setRealTimeTemp(ZhjdIotUtils.subByDecimalPlaces(1,realTimeTemp));
//
//                i += 2;
//                entity.setMin(Integer.parseInt(dataList.get(i),16));
//                i++;
//                entity.setSecond(Integer.parseInt(dataList.get(i),16));
//                save(entity);
//                //结果数据
//            } else if (StringUtils.equals(ZhjdIotUtils.DATA_TYPE_RES, dataList.get(i))) {
//                AsphaltCombustionFurnaceResEntity resEntity = new AsphaltCombustionFurnaceResEntity();
//                resEntity.setId(currentMap.get(deviceId));
//                currentMap.remove(deviceId);
//                resEntity.setDeviceId(deviceId);
//                resEntity.setOriginalData(resData);
//                resEntity.setCreateTime(nowDate);
//
//                // 将16进制转换为10进制数据，不含（0x）
//                // 初始重量WEIGHT1，4字节无符号数，高字节在前，单位g，包括1位小数；
//                i++;
//                String originalWeight = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16) + Integer.parseInt(dataList.get(i + 2),16) + Integer.parseInt(dataList.get(i + 3),16);
//                resEntity.setOriginalWeight(ZhjdIotUtils.subByDecimalPlaces(1,originalWeight));
//
//                //实时重量WEIGHT2，4字节无符号数，高字节在前，单位g，包括1位小数；
//                i += 4;
//                String realTimeWeight = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16) + Integer.parseInt(dataList.get(i + 2),16) + Integer.parseInt(dataList.get(i + 3),16);
//                resEntity.setRealTimeWeight(ZhjdIotUtils.subByDecimalPlaces(1,realTimeWeight));
//
//                i += 2;
//                String realTimeTemp = "" + Integer.parseInt(dataList.get(i),16) + Integer.parseInt(dataList.get(i + 1),16);
//                resEntity.setRealTimeTemp(ZhjdIotUtils.subByDecimalPlaces(1,realTimeTemp));
//
//                i += 2;
//                resEntity.setMin(Integer.parseInt(dataList.get(i),16));
//                i++;
//                resEntity.setSecond(Integer.parseInt(dataList.get(i),16));
//
//                asphaltCombustionFurnaceResService.saveOrUpdate(resEntity);
//            } else {
//                throw new ServiceException("燃烧法沥青含量测定仪（燃烧炉）数据解析异常：" + resData);
//            }
//        } catch (Exception e) {
//            errorRecordService.saveErrorData(ZhjdIotUtils.ASPHALT_COMBUSTION_FURNACE,deviceId,resData);
//            throw new ServiceException("数据解析异常");
//        }

    }
}
